Welcome to Creating Callback Functions. After watching this video, you should be able
to create a callback function to intercept hypertext
transfer protocol (HTTP) method calls. As an asynchronous framework, Node.js makes
extensive use of callback functions to return the result to the calling function. Node.js modules in the software development
kit (SDK) pass an error object as the first parameter in a callback function. Here, the function is defined with an error
as the first parameter. With this convention, the callback function
checks if the first parameter holds an error object. If error is defined, the callback function
handles the error and cleans up any open network or database connections. If the error is not defined, then the callback
function examines the result from the call. If the error is defined, print the error message. Otherwise, the weather.current function call
completed successfully. Print the result from the function call. The codes are in the main app, which has a
weather object (the Node.js module) that calls the weather’s current function. The location is an input parameter: in this
example, an airport. To print temp_f in the browser, we can use
response.end(`The current weather reading is ${temp_f} degrees’). Now we’ll look at an example of passing
an error object to the callback function. Recall how callback functions check the first
parameter to see if an error condition occurred. Instead of printing the result in the console,
you call the resultCallback callback function with the error object. You pass back the error
object to the resultCallback callback function of the main application. If no error occurred, you call the resultCallback
function with null as the first parameter. The codes are in the custom Node.js module. The callback handler printed the contents
of the HTTP response message body to the console. What if you wanted it to return the response
message to the original calling application? If you use a return function, Node.js might call the callback function
after the http.request() call completes. The application calls the exported function. The module that implements the function
calls http.request so that the Node.js framework can make a web service call on its behalf. When that request is sent successfully, the
framework returns control to the Node.js module. Then the Node.js module returns control to
the application. When the remote server sends back a HTTP response
message to the Node.js framework, the framework calls the callback handler that was defined
by the Node.js module. However, there is no connection between the
callback function and the main application. So, how do you link the callback function to
the main application? The pattern is that when one Node.js application
calls a module in a non-blocking manner, the application provides a callback function to
process the result. If the main application calls http.request(),
it must provide a callback handler to process the HTTP response message. If the main application calls a function that
calls http.request(), there are two callback functions: The custom module has a callback function
that handles the HTTP response message from http.request(). And the main application has a callback function
that processes the result captured in the first callback function. Let’s see how this solution works. We’ll create a callback function to capture
the result from the http.request function call. The application makes a call to the Node.js
module. The Node.js module makes an http.request function
call in order to send an HTTP request message to a remote server. Before the remote service returns an HTTP
response message, the http.request function call returns control to the Node.js module
as the request message was successfully sent. Then the Node.js module replies with a value
to the main application. At a future point, the remote server sends
back an HTTP response message. The Node.js framework calls the callback function
defined by the Node.js module. This callback function calls another callback
function defined by the main application. Having one callback function invoke another
callback function is the only way to pass a message from the Node.js module to the main
application when the Node.js module receives a response message. Here, when the main application
calls weather.current(), it passes an anonymous callback function to process the result from the call. In this case, the anonymous function “function
(temp_f)” takes in one input parameter, temp_f. The purpose of this callback function
is to take the weather reading in degrees Fahrenheit and print the results in the console
log. The resultCallback callback function in the
function of the custom Node.js module links to the anonymous callback function, function
(temp_f), of the weather object’s .current function in the main application. Now you can see a Node.js module that returns
a result to the main application with a callback function. Here, a function is defined for the property
named "current." This property will be exported as part of
the module. The anonymous function takes a
parameter named "resultCallback" from the main application. This is how you pass a reference to the main
application's callback function to the Node.js module's callback function. The resultCallback parameter stores the anonymous
callback function from the main application. Look at the bottom of the current property. A "response.on('end')" event handler handles
the transmission of the HTTP response message. When the remote server finishes sending back
the response message, the code makes a call to resultCallback and passes it the current
weather reading in degrees Fahrenheit. This is how you pass a value from one callback
handler to another. In this video, you learned that Node.js makes extensive use of callback functions
to return the result to the calling function. Node.js modules in the SDK pass an error object
as the first parameter in a callback function, and there is one callback function
at each level.